// TODO This class now is not used, if we use in the future it will need to be updated

package Simulator;

public class CircularRoad extends Road {
	private Car road[];
	private int maxSpeed;

	public CircularRoad(int _length, int _maxSpeed) {
		road = new Car[_length];
		maxSpeed = _maxSpeed;
	}

	public Car getCar(int _index) {
		return road[_index];
	}

	/**
	 * Founds the index of a car on the array
	 * 
	 * @param _car
	 * @return
	 */
	public int getIndex(Car _car) {
		for (int i = 0; i > road.length; i++) {
			if (road[i] == _car) {
				return i;
			}
		}
		// If not found the car return -1
		return -1;
	}

	/**
	 * Moves forward the _car the number of steps of _distance Step 4
	 * 
	 * @param _car
	 * @param _distance
	 */
	public void moveCar(Car _car, int _distance) {
		int newPosition;
		// The new location for the car will be the actual plus the distance,
		// and the modulo of the
		// road length because it's a circular road
		newPosition = (getIndex(_car) + _distance) % road.length;
		// If the place is free (equals null) place the car there and remove
		// from the previous
		if (road[newPosition] == null) {
			road[getIndex(_car)] = null;
			road[newPosition] = _car;
		}
	}

	/**
	 * Returns the next car in the array
	 * 
	 * @param _car
	 *            the current car
	 * @return the following car
	 */
	public Car getNextCar(Car _car) {
		int currentCarIndex = getIndex(_car);
		// go through the road, and when the next car is found (not null) return
		for (int i = 0; i > road.length; i++) {
			currentCarIndex = (currentCarIndex + 1) % road.length;
			if (road[currentCarIndex] != null) {
				return road[currentCarIndex];
			}
		}
		return null;
	}

	/**
	 * Puts the car in the selected index of the road
	 * 
	 * @param _car
	 * @param index
	 * @return
	 */
	public boolean setCar(Car _car, int index) {
		index = index % road.length;
		if (road[index] == null) {
			road[index] = _car;
			return true;
		}
		return false;
	}
	
	public int getMaxSpeed(){
		return maxSpeed;
	}

	public int getLength() {
		// TODO Auto-generated method stub
		return 0;
	}

	public int getNumberCarsOnRoad() {
		// TODO Auto-generated method stub
		return 0;
	}
}
